Functionally Oriented Programming¶
lambda¶
Anonimous function
f = lambda x : x + 5
print(f(5)) # 10
a = [x**x for x in range(1, 7)]
print(a) # [1, 4, 27, 256, 3125, 46656]
A = [1, 2, 3, 4, 5, 6]
a = [*map(lambda x: x**x, A)]
print(a) # [1, 4, 27, 256, 3125, 46656]
Sometimes, when we want to iterate over a list, a few methods come in handy.
filter()¶
Filter lets us filter in some values based on conditional logic.
list(filter(lambda x: x > 5, range(8))) # [6, 7]
A = [*map(lambda x: x**x, [1, 2, 3, 4, 5, 6])]
print(A) # [1, 4, 27, 256, 3125, 46656]
b = [*filter(lambda x: x % 2 == 0, A)]
print(b) # [4, 256, 46656]
b1 = [*filter(lambda x: x % 5 == 0, A)]
print(b1) # [3125]
b2 = [*filter(lambda x: x % 2 == 0, map(lambda x: x**x, [1, 2, 3, 4, 5, 6]))]
print(b2) # 4, 256, 46656]
Fib = [0,1,1,2,3,5,8,13,21,34,55]
odd_numbers = list(filter(lambda x: x % 2, Fib))
print(odd_numbers) # [1, 1, 3, 5, 13, 21, 55]
even_numbers = list(filter(lambda x: x % 2 == 0, Fib))
print(even_numbers) # [0, 2, 8, 34]
map()¶
Map applies a function to every element in an iterable.
list(map(lambda x: x**2, range(8))) # [0, 1, 4, 9, 16, 25, 36, 49]
def sum(a, b):
return a + b
a = list(map(sum, [1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]))
print(a) # [8, 10, 12, 14, 16, 18]
A = [1, 2, 3, 4, 5, 6]
B = [7, 8, 9, 10, 11, 12]
a = list(map(sum, A, B))
print(a) # [8, 10, 12, 14, 16, 18]
a = list(map(lambda x, y: x + y, A, B))
print(a)
Celsius = [39.2, 36.5, 37.3, 37.8]
Fahrenheit = [*map(lambda x: (float(9)/5)*x + 32, Celsius)]
print(Fahrenheit) # [102.56, 97.7, 99.14, 100.03999999999999]
C = [*map(lambda x: (float(5)/9)*(x-32), Fahrenheit)]
print(C) # [39.2, 36.5, 37.300000000000004, 37.8]
print(['%.1f' % c for c in C]) # ['39.2', '36.5', '37.3', '37.8']
# get numbers (digits) from string
import re
S = 'Strings: 1,072, Another String: 474'
for x in [*(map(int, re.findall('\d+', S)))]:
print(x)
# array calculations
a = [1, 2, 3, 4]
b = [17, 12, 11, 10]
c = [-1, -4, 5, 9]
print(list(map(lambda x, y : x + y, a, b))) # [18, 14, 14, 14]
print(list(map(lambda x, y, z : x + y + z, a, b, c))) # [17, 10, 19, 23]
print(list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c))) # [37.5, 33.0, 24.5, 21.0]
# use family of functions
from math import sin, cos, tan, pi
def map_functions(x, functions):
return [ func(x) for func in functions ]
family_of_functions = (sin, cos, tan)
print(map_functions(pi, family_of_functions)) # [1.2246467991473532e-16, -1.0, -1.2246467991473532e-16]
reduce()¶
Reduce repeatedly reduces a sequence pair-wise until we reach a single value
from functools import reduce
reduce(lambda x, y: x - y, [1,2,3,4,5]) # -13
from functools import reduce
A = [47, 11, 42, 102, 13]
# max value
max_val = reduce(lambda x, y: x if (x > y) else y, A)
print(max_val) # 102
# sum of values
sum_of_vals = reduce(lambda x, y: x + y, A)
print(sum_of_vals) # 215
sum_of_vals = reduce(lambda x, y: x + y, range(1, 101))
print(sum_of_vals) # 5050
# factorial
fact = reduce(lambda x, y: x * y, range(1,6))
print(fact) # 120 = 1*2*3*4*5